// pages/rate/rate.js
var app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    rateItem: {
      desc: '',
      td: 0,
      xl: 0,
      res: 0,
      imgs:[]
    },
    orderId:'',
    cleanInfo: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      orderId: options.order_id
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.initInfo()
  },

  /**
   * 
   */
  initInfo: function() {
    let that = this
    app.getAccessToken(app.getPageUrl(getCurrentPages())).then(function (accessToken) {
      wx.request({
        url: app.globalData.hostAPI + '/v1/order/comment-page?access_token=' + accessToken,
        data: {
          out_trade_no: that.data.orderId
        },
        success: res => {
          if(res.data.status == 1) {
            that.setData({
              cleanInfo: res.data.data
            })
          }
        }
      })
    })
  },

  /**
   * inputValue
   */
  inputValue: function(e) {
    this.setData({
      'rateItem.desc': e.detail.value
    })
  },

  /**
   * rate
   */
  rate: function(e) {
    let that = this,
      type = e.currentTarget.dataset.type
    if (type == 'td') {
      console.log('评分态度')
      that.setData({
        'rateItem.td': e.detail.value
      })
    }
    if (type == 'xl') {
      console.log('评分效率')
      that.setData({
        'rateItem.xl': e.detail.value
      })
    }
    if (type == 'res') {
      console.log('综合评分')
      that.setData({
        'rateItem.res': e.detail.value
      })
    }
  },
  /**
   * 发表评论
   */
  submit:function(){
    let that=this
    
    if (that.data.rateItem.td == '') {
      wx.showModal({
        title: '提示',
        content: '请选择保洁人员的服务态度',
      })
      return false
    }
    if (that.data.rateItem.xl == '') {
      wx.showModal({
        title: '提示',
        content: '请选择保洁人员的工作效率',
      })
      return false
    }
    if (that.data.rateItem.res == '') {
      wx.showModal({
        title: '提示',
        content: '请选择保洁人员的综合评分',
      })
      return false
    }
    app.getAccessToken(app.getPageUrl(getCurrentPages())).then(function (accessToken) {
      wx.request({
        url: app.globalData.hostAPI + '/v1/order/comment?access_token=' + accessToken,
        method: 'POST',
        data: {
          out_trade_no: that.data.orderId,
          comment: that.data.rateItem.desc,
          attitude: that.data.rateItem.td,
          efficiency: that.data.rateItem.xl,
          comprehensive: that.data.rateItem.res
        },
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        success: res => {
          //console.log(res)
          if (res.data.status === 1) {
            wx.showToast({
              title: '评论成功',
            })
            setTimeout(function () {
              wx.navigateBack()
            }, 2000)
          }
        }
      })
    })
  }
})